/*
https://leetcode-cn.com/problems/set-intersection-size-at-least-two/solution/she-zhi-jiao-ji-da-xiao-zhi-shao-wei-2-by-leetcode/
 */
import java.util.Arrays;

public class Solution757 {
    public int intersectionSizeTwo(int[][] intervals) {
        Arrays.sort(intervals,(a,b)->a[1]==b[1]?b[0]-a[0]:a[1]-b[1]);
        int ans=0;
        int[] f=new int[intervals.length];
        Arrays.fill(f,2);
        for (int i=0;i<intervals.length;i++){
            for (int j=intervals[i][1];j>=intervals[i][1]-f[i]+1;j--){
                for (int k=i+1;k<intervals.length;k++){
                    if (f[k]>0 && j>=intervals[k][0]){
                        f[k]--;
                    }
                }
                ans++;
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution757().intersectionSizeTwo(new int[][]{{1,3},{1,4},{2,5},{3,5}}));
    }
}
